package com.baba.vote.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author lyd
 */
@Data
@Accessors(chain =  true)
public class PagingDto {

    private Integer pageNumber;

    private Integer pageSize;

    private Long count;

    @JsonIgnore
    private Long offset;

    public PagingDto(Integer pageNumber, Integer pageSize) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
        init();
        offset = (long) ((pageNumber - 1) * pageSize);
    }

    private void init() {
        if ((this.pageNumber == null) || (this.pageNumber <= 0)) {
            this.pageNumber = 1;
        }

        if ((this.pageSize == null) || (this.pageSize <= 0)) {
            this.pageSize = 10;
        }
    }

    public Long getBegin() {
        return 1L * (this.pageNumber - 1) * this.pageSize;
    }

    public Integer getBeginInt() {
        return (this.pageNumber - 1) * this.pageSize;
    }

    public Integer getPageCount() {
        int mod = ((this.count % this.pageSize) == 0) ? 0 : 1;
        int divide = (int) (this.count / this.pageSize);

        return divide + mod;
    }
}
